import MSLib
import sys
import pdb
import time

MSLib.glogger.setRootFile('credo.log')

#MSLib.glogger.at(MSLib.Logs.OBJECTS).setlevel(MSLib.SimpleLogger.DEBUG)
#MSLib.glogger.at(MSLib.Logs.OBJECTS).setident('objects')

MSLib.glogger.at(MSLib.Logs.MSAPI).setlevel(MSLib.SimpleLogger.INFO)
MSLib.glogger.at(MSLib.Logs.MSAPI).setident('msapi')
MSLib.glogger.at(MSLib.Logs.MSAPI).setfile('msapi')

MSLib.glogger.at(MSLib.Logs.CREDODB).setlevel(MSLib.SimpleLogger.INFO)
MSLib.glogger.at(MSLib.Logs.CREDODB).setident('credodb')
MSLib.glogger.at(MSLib.Logs.CREDODB).setfile('credodb')

EVENT_TYPES = \
{
 12: 'Z60', #this is a photo too
 20: 'R130',
 21: 'R130',
 22: 'R130',
 24: 'R402', #system on
 25: 'E402', #system off
 26: 'E611',
 54: 'E130',
 55: 'E130',
 56: 'E130',
 89: 'Z60', #this is photo event
 110: 'R402', #system on
 120: 'E402' #system off
}

def main():
    processed = set()
    objects = {}
    while True:
        processing = set()
        MSLib.Objects.fromFile(objects, 'credo_objects.cfg')
        api = MSLib.CarOnlineAPI(EVENT_TYPES)
        api.forceInit(objects)
        api.getPGSMEvents(processed, processing, objects)
        api.getContactIdEvents(processed, processing, objects)
        api.getPhotos(processed, processing, objects)

        credoparams = {'host': 'localhost:49172', 'user': 'sa', 'password':'', 'timeout':'10'}
        credo = MSLib.CredoDB(credoparams)
        credo.writeEvents(processing, objects)

        processed.update(processing)
        time.sleep(20)

if __name__ == '__main__': main()
